package com.itheima.reggie.controller;

import com.itheima.reggie.bean.ShoppingCart;
import com.itheima.reggie.common.R;

import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;


    @DeleteMapping("/clean")
    public R clean(){
        //1. 调用service
        int row = shoppingCartService.delete();

        //2. 判断结果
        if(row > 0 ){
            return R.success("清空购物车成功");
        }else{
            return R.error("清空购物车失败");
        }
    }


    /*
    * 查询购物车
    *
    * */

    @GetMapping("/list")
    public R list(){

        try {
            List<ShoppingCart> list = shoppingCartService.list();
            return R.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }


    }

    /*
     * 添加购物车
     * */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {

        ShoppingCart shoppingCart1 = shoppingCartService.add(shoppingCart);

        return R.success(shoppingCart1);


    }
    /*
     * 删除购物车
     * */
@PostMapping("/sub")
    public R delete(@RequestBody ShoppingCart shoppingCart){

    ShoppingCart list = shoppingCartService.deleteList(shoppingCart);


    return  R.success(list);

}}
